import 'package:bili_caricature/base/base_state.dart';
import 'package:bili_caricature/pages/book_shelf/repository/home_repository.dart';
import 'package:bloc/bloc.dart';
import '../../../models/response_model.dart';
import '../../../widgets/easy_loading.dart';
import '../../bloc/model/cartoon_model_entity.dart';
import '../../bloc/model/cartoon_recommend_entity.dart';
import '../../bloc/model/cartoon_series_entity.dart';
import 'state.dart';

class BiliMineCubit extends Cubit<BiliMineState> {
  BiliMineCubit() : super(BiliMineState());

  /// 并发请求全部数据
  getAllData() async {
    XsEasyLoading.showLoading();
    List<dynamic> list =
        await Future.wait<dynamic>([getMainData(), getSeriesData(), getRecommendData()]);
    XsEasyLoading.dismiss();
    CartoonModelData? mainModel = list[0];
    List<CartoonSeriesDataSeriesComics>? seriesList = list[1];
    List<CartoonRecommendDataInfos>? recommendList = list[2];
    bool isRefreshMainView = true;
    NetState netState = NetState.dataSuccessState;
    emit(state.clone()
      ..mainModel = mainModel
      ..seriesList = seriesList
      ..recommendList = recommendList
      ..netState = netState
      ..isRefreshMainView = isRefreshMainView);
  }

  /// 请求主数据
  getMainData() async {
    ResponseModel responseMode = await HomeRepository.getMainData<CartoonModelData>();
    return responseMode.data;
  }

  /// 请求系列数据
  getSeriesData() async {
    ResponseModel responseModel = await HomeRepository.getSeriesData<CartoonSeriesData>();
    CartoonSeriesData cartoonSeriesData = responseModel.data;
    return cartoonSeriesData.seriesComics;
  }

  /// 请求推荐数据
  getRecommendData() async {
    ResponseModel responseModel = await HomeRepository.getRecommendData<CartoonRecommendData>();
    CartoonRecommendData cartoonRecommendData = responseModel.data;
    return cartoonRecommendData.infos;
  }

  /// 改变透明度
  void changeAppBarAlphaEvent(double alpha, double newOffset) {
    var newStates = state.clone();
    newStates.isRefreshMainView = false;
    newStates.appBarAlpha = alpha;
    newStates.offset = newOffset;
    emit(newStates);
  }
}
